﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace HappyBa.Core.Data
{
    public class ReceivedData
    {
        public IList<Project> Projects { get; set; }
        public IList<Role> Roles { get; set; }
        public IList<EntityType> EntityTypes { get; set; }
        public IList<Bug> Bugs { get; set; }
        public IList<UserStory> UserStories { get; set; }
        public IList<Task> Tasks { get; set; }
        public IList<Impediment> Impediments { get; set; }
        public IList<Assignment> Assignments { get; set; }
        public IList<Comment> Comments { get; set; }
        public IList<Time> Times { get; set; }
        public IList<User> AllUsers { get; set; }

        public ReceivedData()
        {
            Projects = new Project[0];
            Roles = new Role[0];
            EntityTypes = new EntityType[0];
            Bugs = new Bug[0];
            UserStories = new UserStory[0];
            Tasks = new Task[0];
            Impediments = new Impediment[0];
            Assignments = new Assignment[0];
            Comments = new Comment[0];
            Times = new Time[0];
            AllUsers = new User[0];
        }

        public EntityType GetEntityType(int id)
        {
            return EntityTypes.Single(x => x.Id == id);
        }

        public IEnumerable<Assignment> GetAssignments(int id)
        {
            return Assignments.Where(x => x.Assignable.Id == id);
        }

        public Role GetRole(int id)
        {
            return Roles.Single(x => x.Id == id);
        }

        public IEnumerable<Bug> GetUserStoryBugs(int id)
        {
            return Bugs.Where(x => x.UserStory!=null && x.UserStory.Id == id);
        }

        public IEnumerable<Task> GetTasks(int id)
        {
            return Tasks.Where(x => x.UserStory.Id == id);
        }

        public IEnumerable<Bug> GetProjectBugs(Project project)
        {
            return Bugs.Where(x => x.UserStory == null && FilterByProject(x, project));
        }

        public IEnumerable<UserStory> GetUserStories(Project project)
        {
            return UserStories.Where(x => FilterByProject(x, project));
        }

        public IEnumerable<Impediment> GetImpediments(int id)
        {
            return Impediments.Where(x => x.Assignable.Id == id);
        }

        public IEnumerable<Comment> GetComments(int id)
        {
            return Comments.Where(x => x.General.Id == id);
        }

        public IEnumerable<Time> GetTimes(int userid)
        {
            return Times.Where(x => x.User.Id == userid);
        }

        private static bool FilterByProject(EstimatableEntityBase entity, Project project)
        {
            var id = project.Id;
            switch (project.Type)
            {
                case ProjectType.Feature:
                    return entity.Feature != null && entity.Feature.Id == id;
                case ProjectType.Release:
                    return entity.Release != null && entity.Release.Id == id;
                case ProjectType.Iteration:
                    return entity.Iteration != null && entity.Iteration.Id == id;
                default:
                    throw new ArgumentException("Unknown type: " + project.Type);
            }
        }
    }
}
